public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public static ListNode head;
    public static ListNode last;
    public void creatList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
        this.last = node5;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        node.next = head;
        head = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if (head == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public ListNode findIndex(int index){
        ListNode cur = head;
        while (index - 1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
        }
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }

    public ListNode Search(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    /// ////////////////////////////////////////
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = Search(key);
        if (cur == null){
            System.out.println("没有要删除的数字！");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    /// ////////
    public void removeAll(int key){
        if (head == null){
            return;
        }
        ListNode cur = head;
        ListNode prev = Search(key);
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    public void addFirst1(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    public void addLast1(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        last.next = node;
        last = node;
    }
    public ListNode search(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
  /*  public void addIndex1(int index,int data) {
        if (index < 0 || index > size()) {
            throw new no("位置不合法");
        }
        if (index == 0) {
            addFirst1(data);
            return;
        }
        if (size() == index) {
            addLast1(data);
            return;
        }
        ListNode cur = search(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }*/

    public void remove1(int key){
        if (head == null){
            return;
        }
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                //删除头结点
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    //删除中间结点和尾巴结点
                    if (cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }else {

                cur = cur.next;
            }
        }
    }
}
